package cn.imvc.app.models;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import cn.imvc.app.entities.Favorites;
import cn.imvc.app.entities.Goods;
import cn.imvc.app.entities.GoodsPrice;
import cn.imvc.app.entities.GoodsSku;
import cn.imvc.app.entities.Member;
import cn.imvc.app.repositories.FavoritesRepository;
import cn.imvc.app.repositories.GoodsPriceRepository;
import cn.imvc.app.repositories.GoodsRepository;
import cn.imvc.app.repositories.GoodsSkuRepository;

@Service
public class FavoritesModelImpl implements FavoritesModel {
	@Autowired
	private GoodsRepository goodsRepository;
	@Autowired
	private GoodsSkuRepository goodsSkuRepository;
	@Autowired
	private FavoritesRepository favoritesRepository;
	@Autowired
	private GoodsPriceRepository goodsPriceRepository;
	@Autowired
	private GoodsModel goodsModel;
	@Autowired
	private GoodsSkuModel goodsSkuModel;
	
	@Value("${goods.defaultPriceClassCode}")
	private String defaultPriceClassCode;

	@Override
	public Favorites saveFavorite(Integer skuId, Integer goodsId, Member member) {
		Goods goods = goodsRepository.findOne(goodsId);
		GoodsSku goodsSku = goodsSkuRepository.findOne(skuId);
		Favorites favorites = new Favorites();
		favorites.setGoods(goods);
		favorites.setSku(goodsSku);
		favorites.setMember(member);
		favorites.setGoodsImage(goods.getImage());
		favorites.setGoodsName(goods.getName());
		// favorites.setGoodsNum(1);
		GoodsPrice gp = goodsPriceRepository.findPriceBySkuIdAndPriceCode(skuId, defaultPriceClassCode);
		Assert.notNull(gp);
		favorites.setGoodsPrice(gp.getPrice());
		favorites.setSpec(goodsSku.getSpecId());
		//收藏时间
		favorites.setCreatedAt(new Date());
		favoritesRepository.saveAndFlush(favorites);
		goodsModel.retrySaveGoods(goodsId, 0, 0, 1, 0);
		goodsSkuModel.retryMaintainCountsOfGoodsSku(favorites.getSku().getId(), 0, 0, 1, 0);
		return favorites;
	}

	public Favorites getFavoriteByMemberIdAndSkuId(Integer memberId, Integer skuId) {
		return favoritesRepository.findByMemberIdAndSkuId(memberId, skuId);
	}

	public Page<Favorites> findByMemberId(Integer memberId, PageRequest pageRequest) {
		return (Page<Favorites>) favoritesRepository.findByMemberIdOrderByIdDesc(memberId, pageRequest);
	}

	
	public void deleteFavorites(String ids,Member member) throws Exception {
		String[] id=ids.split(",");
		for (int i=0;i<id.length;i++){			
			Favorites favorite=favoritesRepository.getOne(Integer.parseInt(id[i]));
			if(favorite.getMember().getId()!=member.getId()){
				throw new Exception();
			}
			goodsModel.retrySaveGoods(favorite.getGoods().getId(), 0, 0, -1, 0);;
			goodsSkuModel.retryMaintainCountsOfGoodsSku(favorite.getSku().getId(), 0, 0, -1, 0);
			favoritesRepository.delete(favorite.getId());			
		}
		
	}

	
}
